Displaying Connected Posts in a Grid using PHP Loop - php

I've been searching through the forums and Google for a few days on how to resolve this and have yet to find a solution. Any assistance would be greatly appreciated.
Bottom line, I am looking to achieve this:
Speaker Section
Using Firebug to inspect the element, I've determined which classes apply:
<div class="et_pb_section et_pb_section_8 et_pb_with_background et_section_regular">
<div class=" et_pb_row et_pb_row_6">
<div class="et_pb_column et_pb_column_4_4 et_pb_column_10">
<div class="et_pb_blog_grid_wrapper">
<div class="et_pb_blog_grid clearfix et_pb_module et_pb_bg_layout_ et_pb_cpt_loop_archive_0 et_pb_cpt_archive_grid" data-columns="">
<div class="et_pb_row et_pb_row_cpt et_pb_row_4col">
<div class="et_cpt_container_column et_pb_column et_pb_column_1_4 et_pb_column_0">
<div class="et_pb_section et_pb_section_10 et_section_regular">
<div class=" et_pb_row et_pb_row_7">
<div class="et_pb_column et_pb_column_4_4 et_pb_column_11">
<div class="et_pb_module et-waypoint et_pb_image et_pb_animation_off desaturate et_pb_cpt_featured_image2_0 et_always_center_on_mobile et-animated">
<a href="http://meetingoftheminds.org/speaker/scot-rourke"><img src="http://meetingoftheminds.org/wp-content/uploads/2017/03/Scot-Rourke.jpg" alt="">
</a>
</div>
<div class="clearfix et_pb_module et_pb_bg_layout_light et_pb_text_align_left et_pb_cpt_title_0">
<h2 itemprop="name" class="cpt_title page_title entry-title">Scot Rourke</h2>
</div>
<div class="clearfix et_pb_module et_pb_acf_single_item_0">
<div class="sb_mod_acf_single_item clearfix">
<p>‎Chief Information and Transformation Officer, Technology, Innovation & Performance</p>
</div>
</div>
<div class="clearfix et_pb_module et_pb_acf_single_item_1">
<div class="sb_mod_acf_single_item clearfix">
<p>Cuyahoga County</p>
</div>
</div>
</div>
<!-- .et_pb_column -->
</div>
<!-- .et_pb_row -->
</div>
<!-- .et_pb_section -->
</div>
I then tried to tailor this to my PHP Loop but the result is radically different than expected.
Here is the snippet of the original loop:
<?php
// Get the connected posts
$my_connected_posts = Post_Connector::API()->get_children( "sessions-to-speakers", get_the_id() );
// Check
if ( count( $my_connected_posts ) > 0 ) {
// Loop
foreach ( $my_connected_posts as $my_connected_post ) {
// Display the featured image, title with link, job, and company
echo get_the_post_thumbnail( $my_connected_post, 'thumbnail') . "<br/>";
echo "<a href='" . get_permalink( $my_connected_post->ID ) . "'>" . $my_connected_post->post_title . "</a>" . "<br/>";
echo get_field("job", $my_connected_post) . "<br/>";
echo get_field("company", $my_connected_post) . "<br/>";
}
}
?>
Original loop displays posts vertically down the page as shown here:
Connected Posts
Here is the snippet of the modified loop, using the "Speaker Section" classes, which only seems to resize the thumbnails:
<!-- Speaker Section -->
<div class="et_pb_section et_pb_section_8 et_pb_with_background et_section_regular">
<div class=" et_pb_row et_pb_row_6">
<div class="et_pb_column et_pb_column_4_4 et_pb_column_10">
<div class="et_pb_blog_grid_wrapper">
<div class="et_pb_blog_grid clearfix et_pb_module et_pb_bg_layout_ et_pb_cpt_loop_archive_0 et_pb_cpt_archive_grid" data-columns="">
<div class="et_pb_row et_pb_row_cpt et_pb_row_4col">
<?php
// Get the connected posts
$my_connected_posts = Post_Connector::API()->get_children( "sessions-to-speakers", get_the_id() );
// Check
if ( count( $my_connected_posts ) > 0 ) {
// Loop
foreach ( $my_connected_posts as $my_connected_post ) {
// #speaker section
echo '<div class="et_cpt_container_column et_pb_column et_pb_column_1_4 et_pb_column_0"><div class="et_pb_section et_pb_section_10 et_section_regular">';
echo '<div class=" et_pb_row et_pb_row_7">';
echo '<div class="et_pb_column et_pb_column_4_4 et_pb_column_11">';
// Display the featured image, title with link, job, and company
echo '<div class="et_pb_module et-waypoint et_pb_image et_pb_animation_off desaturate et_pb_cpt_featured_image2_0 et_always_center_on_mobile et-animated">';
echo get_the_post_thumbnail( $my_connected_post, 'thumbnail') . "<br/>";
echo '</div>';
echo '<div class="clearfix et_pb_module et_pb_bg_layout_light et_pb_text_align_left et_pb_cpt_title_0">';
echo "<a href='" . get_permalink( $my_connected_post->ID ) . "'>" . $my_connected_post->post_title . "</a>" . "<br/>";
echo '</div>';
echo '<div class="clearfix et_pb_module et_pb_acf_single_item_0">';
echo '<div class="sb_mod_acf_single_item clearfix">';
echo get_field("job", $my_connected_post) . "<br/>";
echo '</div>';
echo '</div>';
echo '<div class="clearfix et_pb_module et_pb_acf_single_item_1">';
echo '<div class="sb_mod_acf_single_item clearfix">';
echo get_field("company", $my_connected_post) . "<br/>";
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
}
}
?>

Related

List of Wordpress Custom Post Type Archives

I have multiple custom post type archives. I have the code that loops through the posts within the custom post type archive template but I need to display the list of all the custom post type archives instead, is this possible?
Current code:
<?php
global $post;
$post_type = get_post_type($post->ID);
$current_post_id = $post->ID;
$current_parent_post = $post->post_parent;
$post_title_tail = array();
//echo "Post type: " . $post_type;
if(in_array($post_type,array("","page","post"))){
$post_type = $post->post_name;
}
$children = get_children( "post_parent={$post->ID}&post_type={$post_type}" );
$menuQuery = new WP_Query("posts_per_page=0&post_type={$post_type}&post_parent=0&order=ASC&orderby=rand");
echo '';
while($menuQuery->have_posts()): $menuQuery->the_post();
$children_av = get_children( "post_parent={$post->ID}&post_type={$post_type}" );
if(count($children_av) > 0){
$have_child = 'small-arrow-down has-child-nav';
} else {
$have_child = 'small-arrow-right';
}
$hide_show = get_post_meta($post->ID);
if(empty($hide_show['lh_hide_nav'][0])){
if($action==get_the_ID()){
echo '';
} else {
echo '
<div class="columns small-5 large-4 item-id-'.get_the_ID().' ' . $have_child .'" onclick="location.href='.get_the_permalink().';">
<div class="row g-0">
<div class="columns small-6 large-6">
<img src="' . get_stylesheet_directory_uri() . '/images/'. $post_type .'-thumbnail.png" alt="'.get_the_title().'" class="w-100 br-10">
</div>
<div class="columns small-6 large-6 p-0">
<h3>
' . $newLabelArray[trim(strtolower(get_the_title()))] . ' '.get_the_title().'
</h3>
</div>
<div class="columns small-12 large-12">
Learn More
</div>
</div>
</div>
';
}
}
endwhile;
wp_reset_query();
echo '';
?>

Displaying data from while loop into html code

I need tips or direction on how can I display data from mysql using echo. But I want to display it in html code. I want to display $row["title"] of first title in mysql instead title1 and $row["content"] of first content in mysql instead content1 and do that for all 3 divs. php code works fine I just can't figure out how to make that possible.
<div class="carousel-inner" style="background-size:cover;">
<div class="item active">
<img src="img/road1.jpg">
<div class="carousel-caption d-none d-md-block">
<h2>title1</h2>
<p>content1</p>
</div>
</div>
<div class="item">
<img src="img/road2.jpg">
<div class="carousel-caption d-none d-md-block">
<h2>title2</h2>
<p>content2</p>
</div>
</div>
<div class="item">
<img src="img/road3.jpg">
<div class="carousel-caption d-none d-md-block">
<h2>title3</h2>
<p>content3</p>
</div>
</div>-->
<?php
session_start();
include_once("db.php");
$sql = "SELECT * FROM news";
$query = mysqli_query($conn, $sql);
if (mysqli_num_rows($query) > 0) {
while($row = mysqli_fetch_assoc($query)) {
echo "<h2>" . $row["title"] . "</h2>";
echo "<p>" . $row["content"] . "</p>";
}
} else {
echo "0 results";
}
?>
You're almost there. Just move the html into the echo of the while loop.
echo '<div class="carousel-inner" style="background-size:cover;">';
$counter = 1;
while($row = mysqli_fetch_assoc($query)) {
echo '
<div class="item ' . ($counter == 1 ? 'active' : '') . '">
<img src="img/road{$counter}.jpg">
<div class="carousel-caption d-none d-md-block">
<h2>' . $row["title"] . '</h2>
<p>' . $row["content"] . '</p>
</div>
</div>';
$counter++;
}
echo '</div>';
The only issue is the image, realistically you'd save the image in the database with the title and content then use the same method but for this simple case lets just use a counter
please note that I change your entire code a little bit to make the desired results...
<div class="carousel-inner" style="background-size:cover;">
<?php
session_start();
include_once("db.php");
$sql = "SELECT * FROM news";
$query = mysqli_query($conn, $sql);
if (mysqli_num_rows($query) > 0) {
while($row = mysqli_fetch_assoc($query)) { ?>
<div class="item active">
<img src="img/road1.jpg">
<div class="carousel-caption d-none d-md-block">
<h2><?php echo $row["title"]; ?></h2>
<p><?php echo $row["content"]; ?></p>
</div>
</div>
<?php
}
} else {
echo "0 results";
}
?>
Also note that I'm repeating just the first image... You need an extra on planning to determine how to handle images in code and then update this one.

Looping results to insert new div with complexities

How can I loop results to insert new div with complexities for each result?
I have the following database table with following columns:
ID | UserID | Title | Introduction | Content | Images | Background | Date | Pinned
I have the following PHP code:
if($latest = $con->query("SELECT * FROM Posts WHERE Pinned='0' LIMIT 4")) {
if($latest->num_rows > 0) {
//<-- result loop here
} else {
echo '<h1 class="alert fade">No Posts</h1>';
}
$latest->close();
};
I would like to format the output as follows:
<div class="post standard" style="background-image:url([1]);">
<a href="view.php?id=[2]">
<div class="shader"></div>
<div class="info">
<h1>[3]</h1>
<p>[4] - [5]</p>
</div>
</div>
[1] - Background
[2] - ID
[3] - Title
[4] - UserID
[5] - Date
How would I accomplish this?
Here is the code which should do what you need. I used just one echo, you can split it into more echoes, or go out of PHP block. Doesn't matter.
<?php
if ($latest->num_rows > 0) {
while ($row = $latest->fetch_assoc()) {
echo '
<div class="post standard" style="background-image:url(' . $row['background'] . ');">
<a href="view.php?id=' . $row['id'] . '">
<div class="shader"></div>
<div class="info">
<h1>' . $row['title'] . '</h1>
<p>' . $row['userID'] . ' - ' . $row['date'] . '</p>
</div>
</div>
';
}
}
?>
Example with putting HTML code outside PHP block.
<?php
if ($latest->num_rows > 0) {
while ($row = $latest->fetch_assoc()) {
?>
<div class="post standard" style="background-image:url('<?php echo $row['background']; ?>');">
<a href="view.php?id=<?php echo $row['id'] ?>">
<div class="shader"></div>
<div class="info">
<h1><?php echo $row['title']; ?></h1>
<p><?php echo $row['userID']; ?> - <?php echo $row['date']; ?></p>
</div>
</div>
<?php
}
}
?>
Well, what I do is the following
let's say that we're inside the loop for example
if($latest = $con->query("SELECT * FROM Posts WHERE Pinned='0' LIMIT 4")) {
if($latest->num_rows > 0) {
//<-- result loop here
// end the php tag here
?>
<div></div>
<!-- you html tags here-->
<?php // reopen your php tag here again
} else {
echo '<h1 class="alert fade">No Posts</h1>';
}
$latest->close();
};

How to display fetched results into 2 different group of divs from one inner-join-where query?

I'm having troubles displaying results into different divs.
I have this html code:
<div class="grid_10 alpha omega">
<div class="grid_5 alpha paddingtop10">
<div class="grid_5" id="titlescontent"><p class="titlebar">Subcat 1</p></div>
<div class="clear"></div>
<div class="grid_5" id="content"><p>Subsubcategories 1</p></div>
<div class="clear"></div>
</div>
<div class="grid_5 omega paddingtop10">
<div class="grid_5" id="titlescontent"><p class="titlebar">Subcat 2</p></div>
<div class="clear"></div>
<div class="grid_5" id="content"><p>Subsubcategories 2</p></div>
<div class="clear"></div>
</div>
<div class="grid_5 alpha paddingtop10">
<div class="grid_5" id="titlescontent"><p class="titlebar">Subcat 3</p></div>
<div class="clear"></div>
<div class="grid_5" id="content"><p>Subsubcategories 3</p></div>
<div class="clear"></div>
</div>
<div class="grid_5 omega paddingtop10">
<div class="grid_5" id="titlescontent"><p class="titlebar">Subcat 4</p></div>
<div class="clear"></div>
<div class="grid_5" id="content"><p>Subsubcategories 4</p></div>
<div class="clear"></div>
</div>
etc ...
</div>
<div class="clear"></div>
I'm using the 960grid system and alpha class is clearing 5px padding to the left while omega is clearing 5px padding to the right. The other classes are just for colors except of paddingtop10.
This is my php code structure I was trying to implement into my html:
if ($stmt = mysqli_prepare($connect, "SELECT subcategories.subcat_name, subsubcategories.subsubcat_name, subcategories.subcat_ID FROM subcategories INNER JOIN subsubcategories ON subcategories.subcat_ID=subsubcategories.subcat_ID WHERE subcategories.cat_ID = ? OR subcategories.extra_cat_ID = ? ORDER BY subcategories.subcat_name, subsubcategories.subsubcat_name ASC")){
mysqli_stmt_bind_param($stmt, "ii", $cat_ID, $cat_ID);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $subcat_name, $subsubcat_name, $subcat_ID);
$lastcat = 0;
while (mysqli_stmt_fetch($stmt)){
if($lastcat != $subcat_ID){
$lastcat = $subcat_ID;
echo "<br>";
echo $subcat_name;
echo "<br>";
echo "<br>";
}
echo $subsubcat_name;
echo "<br>";
}
}
I want to generate the alpha and omega divs through php but I'm not sure how to do this.
I tried this code but it doesn't give me the right result like I want shown in my html example code:
if ($stmt = mysqli_prepare($connect, "SELECT subcategories.subcat_name, subsubcategories.subsubcat_name, subcategories.subcat_ID FROM subcategories INNER JOIN subsubcategories ON subcategories.subcat_ID=subsubcategories.subcat_ID WHERE subcategories.cat_ID = ? OR subcategories.extra_cat_ID = ? ORDER BY subcategories.subcat_name, subsubcategories.subsubcat_name ASC")){
mysqli_stmt_bind_param($stmt, "ii", $cat_ID, $cat_ID);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $subcat_name, $subsubcat_name, $subcat_ID);
$lastsubcat = 0;
while (mysqli_stmt_fetch($stmt)){
echo '<div class="grid_5 alpha paddingtop10">';
echo '<div class="grid_5" id="titlescontent"><p class="titlebar">';
if($lastsubcat != $subcat_ID){
$lastsubcat = $subcat_ID;
echo $subcat_name;
}
echo '</p></div>';
echo '<div class="clear"></div>';
echo '<div class="grid_5" id="content"><p>';
echo $subsubcat_name;
echo '</p></div>';
echo '<div class="clear"></div>';
echo '</div>';
echo '<div class="grid_5 omega paddingtop10">';
echo '<div class="grid_5" id="titlescontent"><p class="titlebar">';
if($lastsubcat != $subcat_ID){
$lastsubcat = $subcat_ID;
echo $subcat_name;
}
echo '</p></div>';
echo '<div class="clear"></div>';
echo '<div class="grid_5" id="content"><p>';
echo $subsubcat_name;
echo '</p></div>';
echo '<div class="clear"></div>';
echo '</div>';
}
}
Any idea how I need to make this work?
Thank you for any help!
The only thing I can think of is to use a counter to track when you're in the first or last iteration. This would work even if the loop has only one item.
$values = mysqli_stmt_fetch($stmt);
$i = 0;
$len = count($values);
while ($values) {
$i++;
...
if ($i == 1) {
// add alpha class
}
...
if ($i == $len) {
// add omega class
}
}
Does that spark any ideas for you?

Syntax help with echo html and variables

I have this code (see below) that I'm having trouble echoing the variables out. On the 5th line is an echo, within that echo is a load of html (which I've escaped the quotation marks) and a load of variables. I can't get the variables to echo out within the main echo.
Update:
Also within there is an onclick that needs to be taken into account.
<?php
if(
in_array("Branding", get_field('categories')) && $grid_title == "Branding"
){
echo "
<div class=\"grid-box\" onclick=\"location.href='<?php echo get_page_link($post->ID) ?>';\" style=\"cursor: pointer;\">
<div class=\"phase-1\">
<img class=\"grid-image\" src=\"<?php echo $fields->thumb_image; ?>\" alt=\"<?php echo $fields->company_name; ?>\" height=\"152\" width=\"210\" />
<div class=\"grid-heading\">
<h2><?php echo $fields->company_name; ?></h2>
<h3><?php echo implode(', ',get_field('categories'));?></h3>
</div>
</div>
<div class=\"phase-2\">
<div class=\"grid-info\">
<h4><?php echo $fields->project_name; ?></h4>
<p><?php echo $fields->description; ?></p>
</div>
<div class=\"grid-heading-hover\">
<h2><?php echo $fields->company_name; ?></h2>
<h3><?php echo implode(', ',get_field('categories'));?></h3>
</div>
</div>
</div>
";
}
?>
You can't use echo within an echo, you just need to concatenate your strings properly like so:
$Content = "
<div class=\"grid-box\" onclick=\"location.href='" . get_page_link($post->ID). "';\" style=\"cursor: pointer;\">
<div class=\"phase-1\">
<img class=\"grid-image\" src=\"" . $fields->thumb_image . "\" alt=\"" . $fields->company_name. "\" height=\"152\" width=\"210\" />
<div class=\"grid-heading\">
<h2> " . $fields->company_name . "</h2>
<h3>" . implode(', ',get_field('categories')) . "</h3>
</div>
</div>
<div class=\"phase-2\">
<div class=\"grid-info\">
<h4>" . $fields->project_name . "</h4>
<p>" . $fields->description . "</p>
</div>
<div class=\"grid-heading-hover\">
<h2>" . $fields->company_name . "</h2>
<h3>" . implode(', ',get_field('categories')). "</h3>
</div>
</div>
</div>";
echo $Content;
You seem to have a little misunderstanding here.
In PHP you can output HTML either by
writing simple HTML outside <?php...?>, and possibly inserting some PHP code inside
using something like echo / print to output a PHP string which includes HTML
But you cannot mix up the two.
So this is not good:
echo "... src=\"<?php echo $fields->thumb_image; ?>\" ...";
Because this is a string, and in a string you can not open a <?php section. You should do something like this instead:
echo "... src=\"{$fields->thumb_image}\" ...";
which is one of the cool ways to insert PHP variables inside a string.
Something like this
echo " ... <?php ... ?> ...";
will not work.
Instead you could "break in and out" of php, like:
...
?>
<div class="grid-box" onclick="location.href='<?php echo get_page_link($post->ID) ?>';" style="cursor: pointer;">
<div class="phase-1"> ... <?php
Or use s/printf
printf(
'<div class="grid-box" onclick="location.href='%s';" style="cursor: pointer;">',
get_page_link($post->ID)
);
Or use string concatenation:
echo "html ...", get_page_link($post->ID), "some more html...";
Or use some kind of templating code.
Just close the php tag after the if and open it before the closing semi-colon.
if(
in_array("Branding", get_field('categories')) && $grid_title == "Branding"
){
?>
<div class="grid-box" onclick="location.href='<?php echo get_page_link($post->ID) ?>';" style="cursor: pointer;">
<div class="phase-1">
<img class="grid-image" src="<?php echo $fields->thumb_image; ?>" alt="<?php echo $fields->company_name; ?>" height="152" width="210" />
<div class="grid-heading">
<h2><?php echo $fields->company_name; ?></h2>
<h3><?php echo implode(', ',get_field('categories'));?></h3>
</div>
</div>
<div class="phase-2">
<div class="grid-info">
<h4><?php echo $fields->project_name; ?></h4>
<p><?php echo $fields->description; ?></p>
</div>
<div class="grid-heading-hover">
<h2><?php echo $fields->company_name; ?></h2>
<h3><?php echo implode(', ',get_field('categories'));?></h3>
</div>
</div>
</div>
<?php
}
?>

Categories