• Resolved ruipdguerra

    (@ruipdguerra)


    I have a slider with 6 featured images from 6 different post’s, from a specific category on wordpress.

    What i’m trying to do is a function where i create 6/10/12 div’s (depending on how many post’s in this category i have published), and inside each div i want to show the titles of each post.

    I have this so far (but isnt working!!):

    HTML/PHP

    global $post;
    
    $count_posts = wp_count_posts();
    $published_posts = $count_posts->publish;
    
    $args = array( 'numberposts' => $published_posts, 'category' => $slidecat );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :  setup_postdata($post);
    ?>
                    <li>
                    <?php
                    if ( has_post_thumbnail() ) {
                        the_post_thumbnail();
                    }
                    ?>
                        <div class="sb-bolas">
                            <?php
                            for ($zz=0; $zz<$published_posts; $zz++) {
                            ?>
                            <div class="bolas-grad">
                            <?php
                            //echo $parent_title = get_the_title($post->post_parent);
                            echo the_title($zz);
                            ?>
                            </div>
                            <?php
                            }
                            ?>
                        </div>
                    </li>
     <?php endforeach;

    Any ideas?

Viewing 2 replies - 1 through 2 (of 2 total)
  • C W (VYSO)

    (@cyril-washbrook)

    I’m confused about why you need to use wp_count_posts with the publish property and then use that to specify the number of posts. If all you’re trying to do is display data from all the published posts in a particular category, then that’ll happen in a standard query if you just specify the category.

    I may have misinterpreted what you’re trying to do, or there may be some requirement that I’m not aware of, but I wouldn’t have thought you would need anything more complicated than this:

    <?php
    $slider_args = array( 'cat' => $slidecat, 'posts_per_page' => -1 );
    $slider_query = new WP_Query( $slider_args );
    
    if( $slider_query->have_posts() ) :
    while( $slider_query->have_posts() ) : $slider_query->the_post(); ?>
    	<li>
    		<?php if ( has_post_thumbnail() ) :
    			the_post_thumbnail();
    		endif; ?>
    		<div class="sb-bolas">
    			<div class="bolas-grad">
    				<?php the_title(); ?>
    			</div>
    		</div>
    	</li>
    <?php endwhile; endif; ?>

    In other words, for each post in the category specified by $slidecat, it echoes the thumbnail and the title. Note that the_title() does not need to be echoed.

    Thread Starter ruipdguerra

    (@ruipdguerra)

    I have found what i needed with this code:

    <?php
      include( '../../../wp-blog-header.php' ); 
    
      $list_posts = get_posts( array( 'numberposts' => -1, 'category' => $slidecat ) );
    
      $list_titles = array();
      foreach( $list_posts as $post ) {
    //	$list_titles[] = $post->post_title;
    //	$list_titles[] = $post->ID;
    $list_titles[] = $post->guid;
    	}
    
    	echo "<pre>"; print_r($list_titles); echo "</pre>";
    ?>

    Thanks anyway!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get the posts title — wordpress’ is closed to new replies.