Webmaster Forum

Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Reply
 
Thread Tools Display Modes
Share |
  #1  
Old 05-08-2013, 02:27 AM
sstoney200's Avatar
sstoney200 sstoney200 is offline
Contributing Member
 
Join Date: 12-03-10
Location: Essex, UK
Posts: 279
iTrader: 0 / 0%
PHP Query Posts

Hi guys,

I've downloaded a theme template for wordpress and it's displaying my 3 newest portfolio items on the homepage using the following:

PHP Code:
<?php query_posts('post_type=portfolio&posts_per_page=3'); if( have_posts() ) : $counter '0'; while( have_posts() ) : the_post(); $counter++; ?>
            <div class="item four columns <?php if($counter == '1'){ echo 'alpha'; } ?>">
                <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
                <h5 class="blacktext extrabold smalltoppadding uppercase"><?php the_title(); ?><span class="right light">0<?php echo $counter?></span></h5>
                <h6 class="blacktext uppercase"><?php echo the_simple_terms(); ?></h6>
                <a href="<?php the_permalink(); ?>" class="blacktext smallfont"><?php echo $option['view_project']; ?></a>
            </div>
        <?php endwhile; endif; wp_reset_query(); ?>
Anyone know how I can edit that to show a specific or 3 of my preferred portfolio items?

Been reading this http://codex.wordpress.org/Function_...ce/query_posts but whatever I do seems to break it, or start displaying blog posts, which is not what I'm after. :rolleyes:
 
Reply With Quote

Advertisement

Advertisement

  #2  
Old 05-08-2013, 07:14 AM
J. H. Rasmussen J. H. Rasmussen is offline
Super Moderator
 
Join Date: 11-11-11
Location: Copenhagen, Denmark
Posts: 6,003
iTrader: 1 / 100%
Have you tried to replace:
PHP Code:
query_posts('post_type=portfolio&posts_per_page=3'
With something like:
PHP Code:
query_posts(array( 'post_type' => 'page''post__in' => array( 2512 ), 'post_type' => 'portfolio''posts_per_page' => '3')) 
[PHP]
?

In this case, "2, 5, 12" would be the IDs of the three specific posts to show.
 
Reply With Quote
  #3  
Old 05-08-2013, 08:05 AM
sstoney200's Avatar
sstoney200 sstoney200 is offline
Contributing Member
 
Join Date: 12-03-10
Location: Essex, UK
Posts: 279
iTrader: 0 / 0%
Thanks for the reply mate!

Well I changed it but that didn't seem to display anything.
 
Reply With Quote
  #4  
Old 05-08-2013, 09:54 AM
J. H. Rasmussen J. H. Rasmussen is offline
Super Moderator
 
Join Date: 11-11-11
Location: Copenhagen, Denmark
Posts: 6,003
iTrader: 1 / 100%
Did you change the IDs with the actually IDs of the posts you want to display?
 
Reply With Quote
  #5  
Old 05-09-2013, 01:58 AM
sstoney200's Avatar
sstoney200 sstoney200 is offline
Contributing Member
 
Join Date: 12-03-10
Location: Essex, UK
Posts: 279
iTrader: 0 / 0%
Quote:
Originally Posted by J. H. Rasmussen View Post
Did you change the IDs with the actually IDs of the posts you want to display?
Yeah I just had 1, 2, 3 to test to start but it didn't show anything
 
Reply With Quote
  #6  
Old 05-09-2013, 08:16 AM
sstoney200's Avatar
sstoney200 sstoney200 is offline
Contributing Member
 
Join Date: 12-03-10
Location: Essex, UK
Posts: 279
iTrader: 0 / 0%
Thanks mate, how would that work?

Not a pro on the PHP front to say the least! lol
 
Reply With Quote
  #7  
Old 05-10-2013, 01:33 AM
sstoney200's Avatar
sstoney200 sstoney200 is offline
Contributing Member
 
Join Date: 12-03-10
Location: Essex, UK
Posts: 279
iTrader: 0 / 0%
Quote:
Originally Posted by J. H. Rasmussen View Post
Have you tried to replace:
PHP Code:
query_posts('post_type=portfolio&posts_per_page=3'
With something like:
PHP Code:
query_posts(array( 'post_type' => 'page''post__in' => array( 2512 ), 'post_type' => 'portfolio''posts_per_page' => '3')) 
[PHP]
?

In this case, "2, 5, 12" would be the IDs of the three specific posts to show.
Actually mate, I was chatting waffle, that worked a treat!

Thanks for your help! I tried to add to your rep, but it appears you must of helped me in the past as I cannot add again.

Last edited by sstoney200; 05-10-2013 at 01:44 AM.
 
Reply With Quote
  #8  
Old 05-10-2013, 03:47 AM
sstoney200's Avatar
sstoney200 sstoney200 is offline
Contributing Member
 
Join Date: 12-03-10
Location: Essex, UK
Posts: 279
iTrader: 0 / 0%
OK delving a bit deeper...

On my main portfolio page it lists all my portfolio items in that category using the following:

PHP Code:
<?php $the_taxonomy get_post_meta($post->ID'_cmb_the_taxonomy_category',true );
            
            if( 
$the_taxonomy !='-1' ) {
                
query_posts(array( 
                    
'post_type' => 'portfolio',
                    
'showposts' => -1,
                    
'tax_query' => array(
                        array(
                            
'taxonomy' => 'portfolio-category',
                            
'terms' => $the_taxonomy,
                            
'field' => 'term_id',
                        )
                    ),
                    
'orderby' => 'date',
                    
'order' => 'DESC' )
                ); } else { 
query_posts('post_type=portfolio&posts_per_page=-1'); }
                
                 if( 
have_posts() ) : $counter '0'; while( have_posts() ) : the_post(); $counter++; ?>
                <div class="greyvertical midtopmargin item leftpadding rightpadding">
                    <?php the_post_thumbnail(); ?>
                    <h5 class="whitetext extrabold icon-circle-arrow-right smalltoppadding">&nbsp; <?php the_title(); ?><span class="right light">0<?php echo $counter?></span></h5>
                    <h6 class="whitetext leftpadding"><?php echo the_simple_terms(); ?></h6>
                    <div class="meta leftpadding smalltoppadding">
                        <?php the_excerpt(); ?>
                    </div>
                    <a href="<?php the_permalink(); ?>" class="whitetext smallfont leftpadding"><?php echo $option['view_project']; ?></a>
                </div>
            <?php endwhile; endif; wp_reset_query(); ?>
Is there anyway I can randomise this display?
 
Reply With Quote
  #9  
Old 05-10-2013, 05:25 PM
J. H. Rasmussen J. H. Rasmussen is offline
Super Moderator
 
Join Date: 11-11-11
Location: Copenhagen, Denmark
Posts: 6,003
iTrader: 1 / 100%
Quote:
Originally Posted by sstoney200 View Post
OK delving a bit deeper...

On my main portfolio page it lists all my portfolio items in that category using the following:

PHP Code:
<?php $the_taxonomy get_post_meta($post->ID'_cmb_the_taxonomy_category',true );
            
            if( 
$the_taxonomy !='-1' ) {
                
query_posts(array( 
                    
'post_type' => 'portfolio',
                    
'showposts' => -1,
                    
'tax_query' => array(
                        array(
                            
'taxonomy' => 'portfolio-category',
                            
'terms' => $the_taxonomy,
                            
'field' => 'term_id',
                        )
                    ),
                    
'orderby' => 'date',
                    
'order' => 'DESC' )
                ); } else { 
query_posts('post_type=portfolio&posts_per_page=-1'); }
                
                 if( 
have_posts() ) : $counter '0'; while( have_posts() ) : the_post(); $counter++; ?>
                <div class="greyvertical midtopmargin item leftpadding rightpadding">
                    <?php the_post_thumbnail(); ?>
                    <h5 class="whitetext extrabold icon-circle-arrow-right smalltoppadding">&nbsp; <?php the_title(); ?><span class="right light">0<?php echo $counter?></span></h5>
                    <h6 class="whitetext leftpadding"><?php echo the_simple_terms(); ?></h6>
                    <div class="meta leftpadding smalltoppadding">
                        <?php the_excerpt(); ?>
                    </div>
                    <a href="<?php the_permalink(); ?>" class="whitetext smallfont leftpadding"><?php echo $option['view_project']; ?></a>
                </div>
            <?php endwhile; endif; wp_reset_query(); ?>
Is there anyway I can randomise this display?
Try to replace:
PHP Code:
                    'orderby' => 'date',
                    
'order' => 'DESC' 
With:
PHP Code:
                    'orderby' => 'RAND()'
That should return the rows from the table in random order, if i understand you correct.
 
Reply With Quote
  #10  
Old 05-11-2013, 03:17 AM
sstoney200's Avatar
sstoney200 sstoney200 is offline
Contributing Member
 
Join Date: 12-03-10
Location: Essex, UK
Posts: 279
iTrader: 0 / 0%
Quote:
Originally Posted by J. H. Rasmussen View Post
Try to replace:
PHP Code:
                    'orderby' => 'date',
                    
'order' => 'DESC' 
With:
PHP Code:
                    'orderby' => 'RAND()'
That should return the rows from the table in random order, if i understand you correct.
Hi mate,

Yeah I done a bit of searching before posting up so already tried that route, didn't appear to work though. :/
 
Reply With Quote
  #11  
Old 05-11-2013, 02:34 PM
J. H. Rasmussen J. H. Rasmussen is offline
Super Moderator
 
Join Date: 11-11-11
Location: Copenhagen, Denmark
Posts: 6,003
iTrader: 1 / 100%
Have you tried to replace:
PHP Code:
query_posts('post_type=portfolio&posts_per_page=-1'
With:
PHP Code:
query_posts('post_type=portfolio&posts_per_page=-1&orderby=RAND()'
I don't know which of the queries is being used.
 
Reply With Quote
  #12  
Old 05-14-2013, 01:50 AM
sstoney200's Avatar
sstoney200 sstoney200 is offline
Contributing Member
 
Join Date: 12-03-10
Location: Essex, UK
Posts: 279
iTrader: 0 / 0%
Quote:
Originally Posted by J. H. Rasmussen View Post
Have you tried to replace:
PHP Code:
query_posts('post_type=portfolio&posts_per_page=-1'
With:
PHP Code:
query_posts('post_type=portfolio&posts_per_page=-1&orderby=RAND()'
I don't know which of the queries is being used.
Hi buddy,

Sorry about the slow response, I've been on leave.

OK so I've tried both variations :

PHP Code:
<?php $the_taxonomy get_post_meta($post->ID'_cmb_the_taxonomy_category',true );
    
    if( 
$the_taxonomy !='-1' ) {
        
query_posts(array( 
            
'post_type' => 'portfolio',
            
'showposts' => -1,
            
'tax_query' => array(
                array(
                    
'taxonomy' => 'portfolio-category',
                    
'terms' => $the_taxonomy,
                    
'field' => 'term_id',
                )
            ),
          )
        ); } else { 
query_posts('post_type=portfolio&posts_per_page=-1&orderby=RAND()'); }
and

PHP Code:
<?php $the_taxonomy get_post_meta($post->ID'_cmb_the_taxonomy_category',true );
    
    if( 
$the_taxonomy !='-1' ) {
        
query_posts(array( 
            
'post_type' => 'portfolio',
            
'showposts' => -1,
            
'tax_query' => array(
                array(
                    
'taxonomy' => 'portfolio-category',
                    
'terms' => $the_taxonomy,
                    
'field' => 'term_id',
                )
            ),
            
'orderby' => 'RAND()')  
        ); } else { 
query_posts('post_type=portfolio&posts_per_page=-1'); }
but neither are working. Could it be something else?
 
Reply With Quote
  #13  
Old 05-15-2013, 06:18 AM
sstoney200's Avatar
sstoney200 sstoney200 is offline
Contributing Member
 
Join Date: 12-03-10
Location: Essex, UK
Posts: 279
iTrader: 0 / 0%
I managed to get it working using the following:

PHP Code:
    <?php  $the_taxonomy get_post_meta($post->ID'_cmb_the_taxonomy_category',true );
remove_all_filters('posts_orderby');
    if( 
$the_taxonomy !='-1' ) {
        
query_posts(array( 
                          
'orderby' => 'rand',
            
'post_type' => 'portfolio',
            
'showposts' => -1,
            
'tax_query' => array(
                array(
                    
'taxonomy' => 'portfolio-category',
                    
'terms' => $the_taxonomy,
                    
'field' => 'term_id',
                )
            ),
            )    
        ); } else { 
query_posts('post_type=portfolio&posts_per_page=-1'); }
 
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hello Everyone and also a query mrb1129 V7N Forum Support 1 02-03-2013 08:54 PM
Query about SMM bobwarner01 SEO Forum 6 11-04-2011 11:30 PM
Sql Query itHighway Web Design Lobby 1 03-17-2008 12:09 PM
SQL Query Sketch Coding Forum 2 10-13-2004 03:48 PM


V7N Network
Get exposure! V7N I Love Photography V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 05:48 AM.
Powered by vBulletin
Copyright 2000-2014 Jelsoft Enterprises Limited.
Copyright © 2003 - 2018 VIX-WomensForum LLC