How do I display the function the_content in this loop? - php

I have this loop in my wordpress site which displays the latest posts with their titles, I want to display the post content after the title. I normally retrieve it with the function get_the_content but I jkeep breaking the whole page in all kind of ways, I'm very new to php and have little idea of how to add anything to the loop. This is the loop:
while ( $q_query->have_posts() )
{
$q_query->next_post();
$question = get_post($q_query->post);
$loophtml = $loophtml . "<li><span class='list-question-title'>" . "<a class='list-answer-link' href='" . get_permalink($question->ID) ."'>" . $question->post_title . "</a></span>";
$loophtml = $loophtml . "<span class='list-number-answers'>" . get_comments_number($question->ID) . " comentarios</span> ·&nbsp<a href='" . get_permalink($question->ID) ."'>Comentar</a>";
$loophtml = $loophtml . "</li>";
}
Anyone knows how I add the content somewhere? Thanks

All the post data are stored in the $question object, to retrieve the content
echo $question->post_content;
Here is some more information on get_post().
It has nothing to do with your question but please note: next_post() is deprecated.

Related

Change date format for variable and get data from other variable

I'm a rookie and this is my first post. I'm trying to learn PHP. I have created a plugin in Wordpress, getting data from an external API. The connection is working, however, I would like to change the time format for the variable "application_deadline". Do you have any idea how I can do that? I would also like to get the city/municipality data from the API, but it doesn't seem to work. I have tried WorkplaceAddress, city and municipality. You can find the API documentation here: https://jobsearch.api.jobtechdev.se/ (under JobAdSearchResult). Any ideas?
$responseData = wp_remote_retrieve_body( $responseBody );
$result = json_decode( $responseData );
$html.= "<div class='all-jobs-wrapper'>";
foreach($result->hits as $showObj) {
$html.= "<div class='job-ad-wrapper'>";
$html.= "<div class='job-ad-title'>" . $showObj->headline . "</div>";
$html.= "<div class='job-ad-city'>" . $showObj->WorkplaceAddress . "</div>";
$html.= "<div class='job-ad-expires'>" . $showObj->application_deadline . "</div>";
$html.= "<div class='job-ad-link'> <a class='btn' href='" . $showObj->webpage_url . "'>Sök jobb</a></div>";
$html.= "</div>";
}
$html.= "</div>";
echo $html;
}
Many thanks in advance!

PHP -displaying 12 items of an associative array with specific key and value

<?php
foreach($catalog as $id=> $item) {
echo get_item_html($id,$item);
}
// here is the functions page code :
function get_item_html($id,$item){
$output = "<li><a href='#'><img src='"
. $item["img"] . "' alt='"
. $item["title"] . "' />"
. "<p>View Details</p>"
. "</a></li>";
return $output;
}
?>
I have used it for displaying product categories i checked several time but I did find any error but it display undefined variable
please help me to find out this solution

Call specific variable based on what another variable is set to.(PHP)

I am not exactly sure how to word this in the form of a short question...the above called up many different answers that were not related, but I apologize if this has been asked before.
I am trying to make a variable name itself based the value of another.
I could simply create if statements for each one, but if I were able to declare the variable the way I want, it would save me about 20-30 lines of code, and make future additions much easier.
Here is a better description.
This is the code that I am using at the moment. It is within a shortcode function for wordpress, to create a button based on user-given parameters.
extract(shortcode_atts(array(
'size' => 'medium',
'link' => '#',
'text' => ''
), $atts));
$large_button_img = of_get_option('large_button_arrow_upload');
$button_pos = of_get_option('button_image_position');
if($button_pos == 'right' && !empty($button_img)){
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $button_pos ."'>" . $text . "<img src='" . $large_button_img . "' id='button_img' alt='button image' /></a>";
}elseif($button_pos == 'left' && !empty($button_img)){
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $button_pos ."'><img src='" . $large_button_img . "' id='button_img' alt='button image' />" . $text . "</a>";
}else
{
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button'>" . $text . "</a>";
}
return $the_button;
In the above:
The function pulls the value of "size", "link" and "text" from the user given shortcode and generates a button. It sets the class based on the size...
At the moment I have it to where the user can set different images for a large, medium and small button.
Question:
IS IT POSSIBLE
to return the name of the image source based on what size is set to.
soooo basically the equivalent of?
img src = '" . $($size)_button_img . "'
Where it places value of $size in the name of the variable that it pulls to tell it which image source to pull afterwards? (so the proper equivalent of the above would produce something like
img src= '" . $large_button_img ."'
or, if the user has medium selected
img src= '" . $medium_button_img . "'
If possible this save myself having to write if-statements for every possible option (basically copy the set of if-ifelse-else from above, everytime I have a new size setting available)...which eventually may become more of an efficiency issue.
Thanks in advance for any help that can be given :)
ALSO
Please ignore any syntax errors in the above set of code...I am working on this as you read this, so more than likely, if you see something wrong, it has already been fixed.
You can simple use function for your logic:
function somethingWithImage($img, $pos) {
if ($pos == 'right' && !empty($button_img)) {
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $pos ."'>" . $text . "<img src='" . $img . "' id='button_img' alt='button image' /></a>";
} elseif ($pos == 'left' && !empty($button_img)) {
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $pos ."'><img src='" . $img . "' id='button_img' alt='button image' />" . $text . "</a>";
} else {
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button'>" . $text . "</a>";
}
return $the_button;
}
And just call it whenever you want with any arguments.
What you are looking for, are variable variable names.
Short example:
$hello_text = 'Hello World!';
$bye_text = 'Goodbye World!';
$varname = 'hello';
echo ${$varname}_text; // Hello World!
// Is the same as:
echo $hello_text; // Hello World!
As I mentioned, you can use interpolation to create variable variables:
$src = ${"{$size}_button_img"};
But may I suggest using arrays instead? You get cleaner and understandable code:
$sizes = array(
'large' => ...,
'medium' => ...,
);
if(isset($sizes[$size]))
$src = $sizes[$size];

About the evil of eval: How to clean up

I'm building a site that will (eventually) be the front-end for a game. I want to be able to dynamically build a list of "powers" that the user is able to purchase. In order to build these lists, I'm using a PHP-based SQL query, then passing it to Javascript for dynamic choices (some powers have prerequisite powers).
I know there's a simpler way to do what I'm doing, but I'm not super concerned with that right now (I will be later, but I'm trying to get this functional and then clean) (again, I know this is non-optimal).
I'm using eval to parse which divs to show, and I want to know how not to.
I'm having issues getting my div names built right in the first place.
Here's my code:
Javascript (separate file)
function upgradeList(passed)
{
var PowerArray = [];
var DetailPowerID = [];
var EscapedPowerID = [];
PowerArray.push([]);
PowerArray = eval(passed);
var OutputThing="";
for (i=0;i<PowerArray.length;i++)
{
DetailPowerID[i] = 'detail' + PowerArray[i][0];
EscapedPowerID[i] = "'" + DetailPowerID[i] + "'";
}
for (i=0;i<PowerArray.length;i++)
{
OutputThing = OutputThing + "<br><a href='#' onClick='showUpgradeDetails(" + DetailPowerID[i] + ")'>" + PowerArray[i][2] + "</a><div class='hidden' id='" +
DetailPowerID[i] + "'>" + PowerArray[i][3] + "</div>"; }
document.getElementById("secondUpgrade").innerHTML=OutputThing;
document.getElementById("secondUpgrade").style.display='block';
}
}
PHP writing HTML and JS:
{$AbleToUpgrade and $UpgradeList are both 2d arrays built from SQL queries)
echo "<script name='UpgradeList'>";
settype($UpgradesListSize[$i],"int");
for ($i=0;$i<count($AbleToUpgrade);$i++)
{
echo "var UpgradeList" . $AbleToUpgrade[$i][0] . " = new Array();";
for ($j=0;$j<=$UpgradesListSize[$i];$j++)
{
echo "UpgradeList" . $AbleToUpgrade[$i][0] . ".push(Array('"
. $UpgradeList[$i][$j][0] . "', '"
. $UpgradeList[$i][$j][1] . "', '"
. $UpgradeList[$i][$j][2] . "', '"
. $UpgradeList[$i][$j][3] . "', '"
. $UpgradeList[$i][$j][4] . "'));";
}
}
echo "</script>";
... and, later...
echo "<div id='SpendUpgrade'>
Select power to upgrade:
<ul>";
for ($i=0;$i<count($AbleToUpgrade);$i++)
{
echo "<li><a href='#' name='UpgradeList" . $AbleToUpgrade[$i][0] . "' onClick='upgradeList(this.name)'>" . $AbleToUpgrade[$i][1] . " - " . $AbleToUpgrade[$i][2] . "</a></li>";
}
echo "</select>
<div id='secondUpgrade' class='hidden'>
</div>
<div id='thirdUpgrade' class='hidden'>
</div>
</div>";
When I load the page, I wind up with generated text like this:
Real Armor
and the corresponding div:
<div class="hidden" id="detail21" style="display: none;">Your armor only works in the Waking</div>
In order to get the div to show (display:block;), I need to call the function like so:
showUpgradeDetails("detail21")
but I can't make JS / PHP write the quotes correctly. Help (with any or all of this?) please!
I found a resolution, and it wasn't JSON.parse().
I changed PowerArray = eval(passed); into PowerArray = window[passed];.
Because passed contains the name of a variable, and is not the variable itself, I couldn't work directly with it. However, because it was a string that held exclusively the name of a globally-defined variable, I could pass it to the window[] construct and have it work.

PHP and HTML Echo

I want to query a result from a table. The problem I run into is if the text is very long it will not auto-wrap to another line"best way i can think to explain it". It displays the text on one line and will go way off to the side of the page if the text is long. How can I get the text to space correctly. Thanks
here is my code:
<?php
echo "<div class=\"item_list2\">";
$get_items = "SELECT * FROM items WHERE category='Art'";
$result = mysql_query($get_items);
//loop! loops through the multiple results of the $get_items query
while($item = mysql_fetch_array($result, MYSQL_ASSOC)){
echo "<b>" . $item['item'] . "</b><br/>" . $item['email'] . "<br/>";
echo $item['price'] . "</b><br/>" . $item['category'] . "</b><br/>" . $item['extra'];
echo "<br/><a href='manage.php?delete=" . $item['id'] . "'><small>[Delete]</small></a><br/><br/>";
}
echo "</div>";
?>
Just use CSS to break it.
.container
{
word-wrap: break-word;
}
maybe you want to add..
nl2br($item['extra']);

Categories